home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / gg243774.zip / RECORD.ZIP / SERVER.C < prev    next >
Text File  |  1992-04-10  |  16KB  |  590 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /* ITSC Redbook OS/2 v2.0 Sample Program                                 */
  4. /*                                                                       */
  5. /* SERVER.C  -  This is the server part of the WPS Record Class          */
  6. /*                                                                       */
  7. /*                                                                       */
  8. /*************************************************************************/
  9.  
  10. #define INCL_DOS
  11. #define INCL_WIN
  12.  
  13. #include <os2.h>
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <stddef.h>
  20. #include <process.h>
  21. #include <memory.h>
  22. #include <sys\types.h>
  23. #include <sys\stat.h>
  24. #include <bsememf.h>
  25.  
  26. #include "msgs.h"
  27. #include "find.h"
  28.  
  29. #define ID_SRV_WND   200
  30. #define ID_FRAME_WND 201
  31. #define ID_MLE_WND   202
  32. #define ID_VIEW_WND   203
  33.  
  34. #define DebugBox(title, text)  WinMessageBox(HWND_DESKTOP,HWND_DESKTOP, \
  35.                                (PSZ) text , (PSZ) title, 0, \
  36.                                MB_OK | MB_INFORMATION )
  37.  
  38. #define Display(szMsg)   WinPostMsg(hwndMLEWindow, MLM_INSERT, (MPARAM)szMsg, (MPARAM)0 )
  39.  
  40.  
  41. typedef struct {
  42.         HWND hwndRequester;
  43.         PID  pidRequester;
  44. } SRV_DATA, *PSRV_DATA;
  45.  
  46. MRESULT EXPENTRY MainWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  47. MRESULT EXPENTRY ServerObjectWinProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  48.  
  49. MRESULT EXPENTRY FindDlgProc(HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2);
  50.  
  51. VOID Message( PSZ szMsg);
  52.  
  53. void _Optlink ServerObjectThread (void *p);
  54.  
  55. HWND hwndServer;
  56. HWND hwndCliArea;
  57. HWND hwndMLEWindow;
  58. HWND hwndFrame;
  59.  
  60. VOID _Optlink SearchThread(VOID *p);
  61.  
  62. BOOL WinCheck(HWND hwnd);
  63. void removeUnwantedChars(CHAR * );
  64.  
  65. HAB  hab;
  66. CHAR szMsgString[200];
  67. CHAR param1[30];
  68.  
  69.  
  70. void main(argc, argv)
  71.    int argc;
  72.    char *argv[];
  73. {
  74.   HMQ  hmq;
  75.   QMSG qmsg;
  76.   ULONG flCreate;
  77.  
  78.   strcpy (param1, argv[1]);
  79.  
  80.   hab = WinInitialize(0);
  81.  
  82.   hmq = WinCreateMsgQueue( hab, 0 );
  83.  
  84.   if ( !WinRegisterClass( hab, (PSZ)"ServerObjectWin",
  85.                           (PFNWP)ServerObjectWinProc,
  86.                           0L, sizeof(PSRV_DATA) )             )
  87.   {
  88.      DebugBox("Error","Register Server Class Failed");
  89.   }
  90.  
  91.  
  92.   if (!WinRegisterClass( hab, (PSZ)"MainWindow",
  93.                         (PFNWP)MainWindowProc,
  94.                         CS_SIZEREDRAW, 0 ))
  95.   {
  96.      DebugBox("Error","Register Server Frame Class Failed");
  97.   }
  98.  
  99.   flCreate = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER | FCF_MINMAX | FCF_SHELLPOSITION ;
  100.  
  101.   hwndServer = WinCreateWindow(HWND_DESKTOP,
  102.                                   "ServerObjectWin",
  103.                                   "", 0L, 0,0,0,0,
  104.                                   HWND_DESKTOP, HWND_TOP, ID_SRV_WND, NULL, NULL);
  105.  
  106.  
  107.   hwndFrame = WinCreateStdWindow( HWND_DESKTOP, 0,
  108.                                   &flCreate, "MainWindow",
  109.                                   "Server", 0,
  110.                                   (HMODULE)0L, ID_FRAME_WND,
  111.                                   &hwndCliArea ) ;
  112.   if(!hwndFrame)
  113.   {
  114.      DebugBox("Error","Create StdWindow Failed");
  115.   }
  116.  
  117.   WinSetWindowText(hwndFrame, "Record Class Server");
  118.  
  119.   WinSetWindowPos( hwndFrame,  HWND_TOP, 0, 0, 250, 200,
  120.                    SWP_SIZE | SWP_ZORDER | SWP_ACTIVATE | SWP_SHOW);
  121.  
  122.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  123.      WinDispatchMsg( hab, &qmsg );
  124.  
  125.   WinDestroyWindow(hwndFrame);
  126.   WinDestroyMsgQueue( hmq );
  127.   WinTerminate( hab );
  128. }
  129.  
  130. MRESULT EXPENTRY MainWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  131. {
  132.  
  133.   switch( msg )
  134.   {
  135.      case WM_CREATE:
  136.         {
  137.             _beginthread( ServerObjectThread, NULL, 9000, NULL);
  138.  
  139.             hwndMLEWindow = WinCreateWindow(hwnd, WC_MLE,
  140.                                             "", MLS_READONLY | MLS_VSCROLL | MLS_HSCROLL | MLS_BORDER,
  141.                                             0,0,0,0,
  142.                                             hwnd, HWND_TOP,
  143.                                             ID_MLE_WND, NULL, NULL);
  144.  
  145.             return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  146.         }
  147.         break;
  148.  
  149.      case WM_SIZE:
  150.         {
  151.             RECTL  rc;
  152.             WinQueryWindowRect(hwnd,&rc);
  153.  
  154.             rc.yTop -= 40L;
  155.  
  156.             /* Make the MLE follow the frame sizing */
  157.             WinSetWindowPos( hwndMLEWindow,  HWND_TOP,
  158.                              rc.xLeft,
  159.                              rc.yBottom,
  160.                              rc.xRight,
  161.                              rc.yTop,
  162.                              SWP_SIZE | SWP_MOVE | SWP_SHOW);
  163.  
  164.  
  165.             return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  166.         }
  167.         break;
  168.  
  169.     case WM_PAINT:
  170.       {
  171.          HPS    hps;
  172.          RECTL  rc;
  173.  
  174.          hps = WinBeginPaint( hwnd, 0L, &rc );
  175.  
  176.          WinFillRect(hps, &rc, CLR_WHITE);
  177.          WinQueryWindowRect(hwnd,&rc);
  178.  
  179.          rc.yBottom = rc.yTop - 40L;
  180.          WinDrawText(hps, (LONG)strlen( szMsgString ),
  181.                      szMsgString, &rc, CLR_BLUE,
  182.                      CLR_WHITE, DT_CENTER | DT_VCENTER);
  183.  
  184.          WinEndPaint( hps );
  185.       }
  186.       break;
  187.  
  188.      case WM_CLOSE:
  189.         {
  190.             WinSendMsg(hwndServer, WM_QUIT, (MPARAM)0, (MPARAM)0 );
  191.             return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  192.         }
  193.         break;
  194.  
  195.      case WMP_DISPLAY_DATABASE_QUERY_DIALOG:
  196.          {
  197.            PVOID pCreateParam;
  198.            pCreateParam = malloc(sizeof(ULONG));
  199.            *((PULONG)pCreateParam) = (ULONG)mp1;
  200.  
  201.            WinLoadDlg(HWND_DESKTOP,   /* parent is desk top          */
  202.                    hwnd,              /* owner                       */
  203.                    FindDlgProc,       /* dialog procedure            */
  204.                    0L,                /* load from resource file     */
  205.                    ID_DLG_FIND,       /* dialog resource id          */
  206.                    pCreateParam );    /* folder to return results    */
  207.                                       /* gets passed in Create param */
  208.          }
  209.          break;
  210.  
  211.     default:
  212.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  213.   }
  214.   return (MRESULT)FALSE;
  215. }
  216.  
  217. VOID Message(PSZ szMsg)
  218. {
  219.    strcpy( szMsgString, szMsg );
  220.    WinInvalidateRect(hwndCliArea, NULL, TRUE);
  221. }
  222.  
  223. VOID SearchThread(VOID *p)
  224. {
  225.    CHAR   *szSearch1, *szSearch2, *sptr;
  226.  
  227.    PVOID   pData;
  228.    ULONG   ObjectSize;
  229.    ULONG   AttributeFlags;
  230.  
  231.    CHAR    szTemp[500];
  232.  
  233.    APIRET  rc;
  234.    USHORT  cFound;
  235.    FILE    *fptr;
  236.    USHORT  i;
  237.  
  238.    PQUERY_DATA pQuery;
  239.  
  240.    PRESULTS_DATA pResults;
  241.    PPERSON_DATA  pPerson;
  242.  
  243.  
  244.    pQuery = (PQUERY_DATA)p;
  245.  
  246.    sptr = szSearch1 = pQuery->szSearch;
  247.  
  248.    if( *(pQuery->szSearch) == '\0' )
  249.    {
  250.       WinPostMsg(hwndCliArea, WMP_DISPLAY_DATABASE_QUERY_DIALOG, (MPARAM)pQuery->folder, (MPARAM)0 );
  251.       rc = DosFreeMem( (PVOID)p );
  252.       if (rc)
  253.       {
  254.           DebugBox("Error","DosFreeMem Failed");
  255.       }
  256.    }
  257.    else
  258.    {
  259.       Display ( "\nReceived Find Msg : ");
  260.       Display ( pQuery->szSearch );
  261.  
  262.       strlwr(sptr);
  263.  
  264.       while(*sptr != '\0')
  265.       {
  266.          if(*sptr == '@')
  267.          {
  268.              *sptr = '\0';
  269.              sptr++;
  270.              szSearch2 = sptr;
  271.          }
  272.          if(*sptr == 42)
  273.          {
  274.              *sptr = '\0';
  275.          }
  276.          sptr++;
  277.       }
  278.  
  279.       /* Allow up to 100 records to be returned. */
  280.       ObjectSize     =  sizeof(RESULTS_DATA) + ( 100 * sizeof(PERSON_DATA) );
  281.  
  282.       AttributeFlags =  OBJ_GIVEABLE | PAG_WRITE | PAG_READ | PAG_COMMIT;
  283.  
  284.       rc = DosAllocSharedMem(&pData, NULL, ObjectSize,AttributeFlags);
  285.       if (rc)
  286.       {
  287.          Display ( "Error : DosAllocSharedMem failed");
  288.       }
  289.  
  290.       pResults = (PRESULTS_DATA) pData;
  291.       pPerson  = (PPERSON_DATA) ( (ULONG)pData + (ULONG)sizeof(RESULTS_DATA) );
  292.  
  293.       pResults->cRecords = 0;
  294.  
  295.       pResults->folder = pQuery->folder;
  296.       strcpy(pResults->szSearch, pQuery->szSearch);
  297.  
  298.       if( (fptr=fopen("C:\\NAMES.DAT","r")) != NULL)
  299.       {
  300.          for(i=0;i<100;i++)
  301.          {
  302.             fgets(pPerson->szName     , 99  , fptr);
  303.             fgets(pPerson->szTelepnone, 39  , fptr);
  304.             fgets(pPerson->szAddress  , 299 , fptr);
  305.  
  306.             if(feof(fptr))
  307.                break;
  308.  
  309.             removeUnwantedChars(pPerson->szName     );
  310.             removeUnwantedChars(pPerson->szTelepnone);
  311.             removeUnwantedChars(pPerson->szAddress  );
  312.  
  313.             /*****************************/
  314.             /* Perform simplified search */
  315.             /*****************************/
  316.  
  317.             strcpy(szTemp, pPerson->szName      );
  318.             strcat(szTemp, pPerson->szTelepnone );
  319.             strlwr(szTemp);
  320.  
  321.  
  322.             if( ( strstr(szTemp,szSearch1) != NULL ) &&  ( strstr(szTemp,szSearch2) ) )
  323.             {
  324.                pResults->cRecords++;
  325.                pPerson++;
  326.             }
  327.          }
  328.          fclose(fptr);
  329.       }
  330.  
  331.  
  332.       if(pResults->cRecords == 0)
  333.       {
  334.          strcpy( (CHAR *)pPerson,"Nothing Found");
  335.       }
  336.  
  337.       rc = DosFreeMem( (PVOID)p );
  338.       if (rc)
  339.       {
  340.           DebugBox("Error","DosFreeMem Failed");
  341.       }
  342.       WinPostMsg(hwndServer, WMP_RESULTS_FROM_SEARCH, (MPARAM)pResults, (MPARAM)0);
  343.    }
  344.  
  345. }
  346.  
  347. BOOL WinCheck(HWND hwnd)
  348. {
  349.    if(! WinIsWindow(hab, hwnd) )
  350.    {
  351.       DosBeep(500,500);
  352.       Display("\n<sys msg> Cannot Access Requester - Results data discarded");
  353.       return(FALSE);
  354.    }
  355.    else
  356.       return(TRUE);
  357. }
  358.  
  359. void removeUnwantedChars(CHAR *string)
  360. {
  361.    CHAR *sptr;
  362.    sptr = string;
  363.    while(*sptr != '\0')
  364.    {
  365.       if(*sptr < 32 )
  366.       {
  367.          *sptr = '\0';
  368.          break;
  369.       }
  370.       sptr++;
  371.    }
  372. }
  373.  
  374.  
  375. /*****************************************************/
  376. /*                Server Thread                      */
  377. /*****************************************************/
  378.  
  379.  
  380. void ServerObjectThread (void *p)
  381. {
  382.   HMQ  hmq;
  383.   QMSG qmsg;
  384.   HAB  hab;
  385.  
  386.   hab = WinInitialize(0);
  387.  
  388.   hmq = WinCreateMsgQueue( hab, 0 );
  389.  
  390.   if ( !WinRegisterClass( hab, (PSZ)"ServerObjectWin",
  391.                           (PFNWP)ServerObjectWinProc,
  392.                           0L, sizeof(PSRV_DATA) )             )
  393.   {
  394.      DebugBox("Error","Register Server Class Failed");
  395.   }
  396.  
  397.  
  398.   hwndServer = WinCreateWindow(HWND_OBJECT,
  399.                                   "ServerObjectWin",
  400.                                   "", 0L, 0,0,0,0,
  401.                                   (HWND)0, HWND_TOP, 0L, NULL, NULL);
  402.  
  403.  
  404.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  405.      WinDispatchMsg( hab, &qmsg );
  406.  
  407.   WinDestroyWindow(hwndServer);
  408.   WinDestroyMsgQueue( hmq );
  409.   WinTerminate( hab );
  410. }
  411.  
  412.  
  413.  
  414. MRESULT EXPENTRY ServerObjectWinProc( HWND hwnd,
  415.                                       ULONG msg,
  416.                                       MPARAM mp1,
  417.                                       MPARAM mp2 )
  418. {
  419.    MRESULT mr;
  420.    SRV_DATA *pServerData;
  421.  
  422.    mr = (MRESULT)FALSE;
  423.  
  424.    switch( msg )
  425.    {
  426.      case WM_CREATE:
  427.        {
  428.           HWND hwndReq;
  429.           hwndReq = (HWND)atol(param1);
  430.           WinPostMsg(hwndReq, WMP_SERVER_READY, (MPARAM)hwnd, (MPARAM)0 );
  431.           WinPostMsg(hwnd, WMP_INITIALISE_SERVER, (MPARAM)hwndReq, (MPARAM)0 );
  432.        }
  433.        break;
  434.  
  435.     case WMP_INITIALISE_SERVER:
  436.       {
  437.          PID pid;
  438.          TID tid;
  439.  
  440.          pServerData = (SRV_DATA *)malloc( sizeof(SRV_DATA) );
  441.  
  442.          pServerData->hwndRequester = (HWND)mp1;
  443.  
  444.          WinQueryWindowProcess((HWND)mp1,&pid,&tid);
  445.          pServerData->pidRequester = pid;
  446.  
  447.          WinSetWindowPtr(hwnd,0,pServerData);
  448.  
  449.          Message("Ready");
  450.          Display("<Note: In a real application this window would not be displayed>");
  451.  
  452.       }
  453.       break;
  454.  
  455.     case WMP_RESULTS_FROM_SEARCH:
  456.        {
  457.          ULONG rc;
  458.          pServerData = WinQueryWindowPtr(hwnd,0);
  459.  
  460.          if( WinCheck(pServerData->hwndRequester) )
  461.          {
  462.             rc = DosGiveSharedMem( (PVOID)mp1,
  463.                                    pServerData->pidRequester,
  464.                                    PAG_WRITE | PAG_READ);
  465.  
  466.             if (rc)
  467.             {
  468.                 DebugBox("Error","DosGiveSharedMem Failed");
  469.             }
  470.             else
  471.             {
  472.                WinSendMsg(pServerData->hwndRequester, WMP_RESULTS_FROM_SEARCH, (MPARAM)mp1, (MPARAM)0);
  473.                /* Free results memory from this process now it has been pass on */
  474.                rc = DosFreeMem( (PVOID)mp1 );
  475.                if (rc)
  476.                {
  477.                    DebugBox("Error","DosFreeMem Failed");
  478.                }
  479.             }
  480.          }
  481.          else
  482.          {
  483.             rc = DosFreeMem( (PVOID)mp1 );
  484.             if (rc)
  485.             {
  486.                 DebugBox("Error","DosFreeMem Failed");
  487.             }
  488.          }
  489.        }
  490.        break;
  491.  
  492.     case WMP_SEARCH_DATABASE:
  493.        {
  494.           _beginthread( SearchThread, NULL, 9000, (PVOID)mp1);
  495.        }
  496.        break;
  497.  
  498.     case WM_CLOSE:
  499.        WinPostMsg( hwndFrame, WM_CLOSE, (MPARAM)0, (MPARAM)0 );
  500.        mr = WinDefWindowProc( hwnd, msg, mp1, mp2 );
  501.        break;
  502.  
  503.     case WM_QUIT:
  504.        pServerData = WinQueryWindowPtr(hwnd,0);
  505.        if(WinIsWindow(hab, pServerData->hwndRequester))
  506.           WinPostMsg(pServerData->hwndRequester, WMP_SERVER_TERMINATED, (MPARAM)hwnd, (MPARAM)0 );
  507.        mr = WinDefWindowProc( hwnd, msg, mp1, mp2 );
  508.        break;
  509.  
  510.      default:
  511.        mr = WinDefWindowProc( hwnd, msg, mp1, mp2 );
  512.    }
  513.    return mr;
  514.  
  515. }
  516.  
  517. MRESULT EXPENTRY FindDlgProc(HWND hwndDlg, ULONG msg,
  518.                                   MPARAM mp1, MPARAM mp2)
  519. {
  520.  
  521.  switch (msg)
  522.  {
  523.  
  524.     case WM_INITDLG:
  525.  
  526.         WinSetWindowULong(hwndDlg,
  527.                           QWL_USER,
  528.                           *((PULONG)mp2) ); /* Store Folder pointer     */
  529.  
  530.         free(mp2);                          /* Free Create Param memory */
  531.         break;
  532.  
  533.     case WM_COMMAND:
  534.        {
  535.           switch (SHORT1FROMMP(mp1))
  536.           {
  537.             case DID_OK:
  538.               {
  539.                  PQUERY_DATA pQuery;
  540.                  PVOID       pData;
  541.                  ULONG       ObjectSize;
  542.                  ULONG       AttributeFlags;
  543.                  CHAR        szTemp[100];
  544.                  APIRET      rc;
  545.  
  546.                  ObjectSize     =  sizeof(QUERY_DATA);
  547.  
  548.                  AttributeFlags =  PAG_WRITE | PAG_READ | PAG_COMMIT;
  549.  
  550.                  rc = DosAllocMem(&pData, ObjectSize, AttributeFlags);
  551.                  if (rc)
  552.                  {
  553.                     DebugBox("Error","DosAllocMem failed");
  554.                  }
  555.  
  556.                  pQuery = (PQUERY_DATA)pData;
  557.  
  558.                  /* Get the stuff the user typed in */
  559.                  WinQueryDlgItemText(hwndDlg,
  560.                                      ID_EF_TELNUMBER,
  561.                                      sizeof(szTemp),
  562.                                      (PSZ)szTemp);
  563.  
  564.                  strcpy( pQuery->szSearch, szTemp);
  565.                  strcat( pQuery->szSearch, "@");
  566.  
  567.                  WinQueryDlgItemText(hwndDlg,
  568.                                      ID_EF_SURNAME,
  569.                                      sizeof(szTemp),
  570.                                      (PSZ)szTemp);
  571.                  strcat( pQuery->szSearch, szTemp);
  572.  
  573.                  pQuery->folder = WinQueryWindowULong(hwndDlg, QWL_USER);
  574.  
  575.                  WinPostMsg(hwndServer, WMP_SEARCH_DATABASE, (MPARAM)pQuery, (MPARAM)0);
  576.               }
  577.               return (MRESULT) TRUE;
  578.               break;
  579.  
  580.             case DID_CANCEL:
  581.               WinDismissDlg(hwndDlg,DID_CANCEL);
  582.               break;
  583.           }
  584.        }
  585.        return (MRESULT) TRUE;
  586.        break;
  587.  }
  588.  return (WinDefDlgProc(hwndDlg, msg, mp1, mp2) );
  589. }
  590.